package com.huang.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huang.eduservice.client.VodClient;
import com.huang.eduservice.entity.EduChapter;
import com.huang.eduservice.entity.EduVideo;
import com.huang.eduservice.entity.chapter.ChapterVo;
import com.huang.eduservice.entity.chapter.VideoVo;
import com.huang.eduservice.mapper.EduChapterMapper;
import com.huang.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huang.eduservice.service.EduVideoService;
import com.huang.servicebase.execptionhandler.huangException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-03-21
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    @Override
    public List<ChapterVo> getChapterVideo(String courseId) {
        //根据id查询章节
        QueryWrapper<EduChapter> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("course_id", courseId);
        //所有的章节
        List<EduChapter> eduChapters = baseMapper.selectList(wrapper1);
        //根据id查询小结id
        QueryWrapper<EduVideo> wrapper2 = new QueryWrapper<>();
        //所有的小节
        wrapper2.eq("course_id", courseId);
        List<EduVideo> eduVideos = videoService.list(wrapper2);
        ArrayList<ChapterVo> chapterVos = new ArrayList<>();
        //遍历章节 将本章节的小节放入数组
        for (int i = 0; i < eduChapters.size(); i++) {
            EduChapter eduChapter = eduChapters.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);
            chapterVos.add(chapterVo);
            ArrayList<VideoVo> videoVos = new ArrayList<>();
            for (int j = 0; j < eduVideos.size(); j++) {
                if (eduVideos.get(j).getChapterId().equals(eduChapter.getId())) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideos.get(j), videoVo);
                    videoVos.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVos);
        }
        return chapterVos;
    }


    @Override
    public void addChapter(EduChapter eduChapter) {
        baseMapper.insert(eduChapter);
    }

    @Override
    public EduChapter getChapterByid(String id) {
        EduChapter eduChapter = baseMapper.selectById(id);
        return eduChapter;
    }

    @Override
    public void updateChapter(EduChapter chapter) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", chapter.getCourseId());
        baseMapper.update(chapter, wrapper);
    }

    @Override
    public String deleteChapter(String id) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", id);
        int count = videoService.count(wrapper);
        if (count != 0) {
            throw new huangException(20001, "有小节不能删除");
        } else {
            baseMapper.deleteById(id);
            return "删除成功";
        }
    }

    @Override
    public void removeChapterByCourseId(String id) {
        QueryWrapper<EduChapter> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("course_id", id);
        baseMapper.delete(wrapper2);
    }


}
